/*
 *   Copyright 1999-2016 Asiainfo Technologies(China),Inc.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.asiainfo.redis.config;

import com.asiainfo.redis.scheduling.RefreshAllBusiTypeDataJob;
import com.asiainfo.redis.scheduling.RefreshChangeBusiDataJob;
import org.quartz.JobDetail;
import org.quartz.Trigger;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.scheduling.quartz.SimpleTriggerFactoryBean;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * @author william.xu
 * @version v 1.0 2015年2月27日 上午11:09:29
 *
 */

@Configuration
@ConfigurationProperties(prefix="quartz")
//@EnableConfigurationProperties
public class ScheduledConfig implements ApplicationContextAware{

	private ApplicationContext applicationContext;
	
	@Resource
	private DataSource dataSource;
	
	private Map quartzProperties = new HashMap();
	
	public Map getQuartzProperties() {
		return quartzProperties;
	}

	@Bean
    public SchedulerFactoryBean schedulerFactory(){
        SchedulerFactoryBean bean = new SchedulerFactoryBean();
        bean.setSchedulerName("MY_CLUSTERED_JOB_SCHEDULER");
        bean.setApplicationContextSchedulerContextKey("applicationContext");
        Iterator<String> it = quartzProperties.keySet().iterator();
        Properties properties = new Properties();
        while (it.hasNext()){
        	String key = it.next();
        	properties.put(key.replaceAll("_", "\\."), quartzProperties.get(key).toString());
        }
        bean.setQuartzProperties(properties);
        bean.setDataSource(dataSource);
        bean.setJobDetails(new JobDetail[]{(JobDetail)applicationContext.getBean("refreshChangeBusiDataJobDetail"),(JobDetail)
				applicationContext.getBean("refreshAllBusiTypeDataJobDetail")});
        bean.setAutoStartup(true);
        bean.setStartupDelay(10);
        
        return bean;
    }


	@Bean
	public JobDetailFactoryBean refreshChangeBusiDataJobDetail(){
		JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
		jobDetailFactoryBean.setDurability(true);
		jobDetailFactoryBean.setJobClass(RefreshChangeBusiDataJob.class);
		return jobDetailFactoryBean;
	}

	@Bean
	public JobDetailFactoryBean refreshAllBusiTypeDataJobDetail(){
		JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
		jobDetailFactoryBean.setDurability(true);
		jobDetailFactoryBean.setJobClass(RefreshAllBusiTypeDataJob.class);
		return jobDetailFactoryBean;
	}

	@Bean
	public CronTriggerFactoryBean refreshChangeBusiDataCronTrigger(){
		CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
		cronTriggerFactoryBean.setCronExpression("0 0/1 * * * ?");//默认每分钟
		cronTriggerFactoryBean.setJobDetail((JobDetail) applicationContext.getBean("refreshChangeBusiDataJobDetail"));
		//cronTriggerFactoryBean.setMisfireInstruction(Trigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY);
		cronTriggerFactoryBean.setMisfireInstruction(Trigger.MISFIRE_INSTRUCTION_SMART_POLICY);
		return cronTriggerFactoryBean;
	}

	@Bean
	public CronTriggerFactoryBean refreshAllBusiTypeDataCronTrigger(){
		CronTriggerFactoryBean cronTriggerFactoryBean = new CronTriggerFactoryBean();
		cronTriggerFactoryBean.setCronExpression("0 0 1 * * ?");//默认每天凌晨1点
		cronTriggerFactoryBean.setJobDetail((JobDetail) applicationContext.getBean("refreshAllBusiTypeDataJobDetail"));
		//cronTriggerFactoryBean.setMisfireInstruction(Trigger.MISFIRE_INSTRUCTION_IGNORE_MISFIRE_POLICY);
		cronTriggerFactoryBean.setMisfireInstruction(Trigger.MISFIRE_INSTRUCTION_SMART_POLICY);
		return cronTriggerFactoryBean;
	}


	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}

	
}
